/**
 * 
 *@
<pre>
 	var user = coos.hashmap();
	user.set("name","zhangsan");
	user.set("age","28");
	user.set("job","leader");
	var result1 = user.getValues().toString();

	var user2 = coos.hashmap();
	user2.set("username","zhangsan");
	user2.set("username","zhangsan");
	var result2 = user2.get("username");
	
	alert(result1 + "========" + result2);
</pre>
 */
coos.hashmap = function()
{
	function hash(_key, _value)
	{
		this.key	= _key;
		this.value	= _value;
	};
	var map = new Array();
	
	return {
		set : function(key,value)
		{
			for (var i = 0; i < map.length; i++)
			{
				if (map[i].key == key )
				{
					map[i].value = value;
					return;
				}
			}
			map[map.length] = new hash(key,value);			
		},
		get : function(key)
		{
			for (var i = 0; i < map.length; i++)
			{
				if (map[i].key == key)
					return map[i].value;
			}
			return null;
		},
		size : function()
		{
			return map.length;
		},
		getKey : function(index)
		{
			if (map.length <= index)
				return null;
			return map[index].key;
		},
		getValue : function(index)
		{
			if (map.length <= index)
				return null;
			return map[index].value;
		},
		getLastKey : function()
		{
			return map[map.length-1].key;
		},
		getLastValue : function()
		{
			return map[map.length-1].value;
		},
		getKeys : function()
		{
			var keys = [];
			map.forEach(function(_hash){
				keys.push(_hash.key);
			});
			return keys;
		},
		getValues : function()
		{
			var values = [];
			map.forEach(function(_hash){
				values.push(_hash.value);
			});
			return values;
		},
		containsKey : function(key)
		{
			return this.getKeys().contains(key);
		},
		containsValue : function(value)
		{
			return this.getValues().contains(value);
		},
		each : function(callback)
		{
			map.forEach(function(_hash){
				callback(_hash.key,_hash.value);
			});
		},
		toString : function()
		{
			var arr = [];
			map.forEach(function(_hash){
				arr.push("\"" + _hash.key + "\":\"" + _hash.value + "\"");
			});
			return "{" + arr.toString(",") + "}";
		},
		toJson : function()
		{
			return this.toString().toJson();
		},
		remove : function(key)
		{
			for(var i = 0; i < map.length; i++)
			{
				if(map[i].key == key)
					map.splice(i,1);
			}
		},
		isEmpty : function()
		{
			return (map.length == 0);
		},
		sort : function()
		{
			map = map.sort();
		},
		clear : function()
		{
			map = [];
		}
	};
};
